package com.kjtpay.design_pattern.factory;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.decorator.DecoratorActivity;

public class FactoryActivity extends AppCompatActivity {
    LeiFeng leiFeng;
    IFactory factory;
    RadioButton radioButtonStudent;
    RadioButton radioButtonVolunteer;
    RadioButton radioButtonPolice;
    TextView textViewInfo;

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, FactoryActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_factory);
        setTitle("工厂方法模式");

        radioButtonStudent = findViewById(R.id.radioButtonStudent);
        radioButtonVolunteer = findViewById(R.id.radioButtonVolunteer);
        radioButtonPolice = findViewById(R.id.radioButtonPolice);
        textViewInfo = findViewById(R.id.textViewInfo);
    }

    public void onSweepClick(View view) {
        buildProduct();
        leiFeng.sweep();
        textViewInfo.setText(leiFeng.info);
    }

    public void onWashClick(View view) {
        buildProduct();
        leiFeng.wash();
        textViewInfo.setText(leiFeng.info);
    }

    public void onBuyRiceClick(View view) {
        buildProduct();
        leiFeng.buyRice();
        textViewInfo.setText(leiFeng.info);
    }

    private void buildProduct() {
        if (radioButtonStudent.isChecked()) {
            factory = new StudentFactory();
        } else if (radioButtonVolunteer.isChecked()) {
            factory = new VolunteerFactory();
        } else if (radioButtonPolice.isChecked()) {
            factory = new PoliceFactory();
        } else {
            // 啥也不干
        }
        if (null != factory) {
           leiFeng = factory.createLeiFeng();
        }
    }
}